package com.ugoodtech.mdcc.core.service.impl;

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.core.Constants;
import com.ugoodtech.mdcc.core.domain.*;
import com.ugoodtech.mdcc.core.dto.JsonResponse;
import com.ugoodtech.mdcc.core.helper.SearchVoId;
import com.ugoodtech.mdcc.core.repository.*;
import com.ugoodtech.mdcc.core.service.CustomerService;
import com.ugoodtech.mdcc.core.service.DepartmentCoreService;
import com.ugoodtech.mdcc.core.service.SearchPermissionsService;
import io.swagger.annotations.ApiParam;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserProfileRepository userProfileRepository;

    @Autowired
    private CustomerRegularReportRepository customerRegularReportRepository;

    @Autowired
    private StationCustomerReportRepository stationCustomerReportRepository;

    @Autowired
    private QuestionAnswerOptionRepository questionAnswerOptionRepository;

    @Autowired
    private DepartmentRepository departmentRepository;

    @Autowired
    private DepartmentCoreService departmentCoreService;

    @Autowired
    private SearchPermissionsService searchPermissionsService;
    @Autowired
    private StationRegularServiceReportRepository stationRegularServiceReportRepository;
    @Autowired
    private StationReportRepository stationReportRepository;
    @Autowired
    private StationColorReportRepository stationColorReportRepository;

    @Override
    public Page<CustomerRegularReport> getCustomerReport(Long loginUserId,
                                                         Pageable pageable,
                                                         @ApiParam("省") @RequestParam(value = "province", required = false) String province,
                                                         @ApiParam("城市") @RequestParam(value = "city", required = false) String city,
                                                         @ApiParam("类型") @RequestParam(value = "category", required = false) String category,
                                                         @ApiParam("品牌") @RequestParam(value = "brand", required = false) String brand,
                                                         @ApiParam("客户状态ID") @RequestParam(value = "customerStat", required = false) String customerStat,
                                                         @ApiParam("客户类型ID") @RequestParam(value = "customerType", required = false) String customerType,
                                                         @ApiParam("油漆品牌ID") @RequestParam(value = "paintBrand", required = false) String paintBrand,
                                                         @ApiParam("品牌定位ID") @RequestParam(value = "productPosition", required = false) String productPosition,
                                                         @ApiParam("服务项目") @RequestParam(value = "serviceType", required = false) String serviceType,
                                                         @ApiParam("服务次数") @RequestParam(value = "serviceNum", required = false) Integer serviceNum,
                                                         @ApiParam("常规服务报告状态") @RequestParam(value = "reportStatus", required = false) String reportStatus,
                                                         @ApiParam("需跟进报告处理状态") @RequestParam(value = "serviceReportStatus", required = false) String serviceReportStatus,
                                                         @ApiParam("所属部门") @RequestParam(value = "department", required = false) String department,
                                                         @ApiParam("是否填写精益钣喷：true:填写，false:未填写") @RequestParam(value = "paintType", required = false) Boolean paintType,
                                                         @ApiParam("该维修站是否使用我司增值服务") @RequestParam(value = "payService", required = false) String payService,
                                                         @ApiParam("油漆配送时效") @RequestParam(value = "sentTime", required = false) String sentTime,
                                                         @ApiParam("经销商上门服务周期") @RequestParam(value = "serviceTime", required = false) String serviceTime,
                                                         @ApiParam("维修站名称") @RequestParam(value = "stationName", required = false) String stationName,
                                                         @ApiParam("服务开始时间") @RequestParam(value = "serviceStartTime", required = false) Long serviceStartTime,
                                                         @ApiParam("服务结束时间") @RequestParam(value = "serviceEndTime", required = false) Long serviceEndTime,
                                                         @ApiParam("提交报告时间") @RequestParam(value = "startTime", required = false) Long startTime,
                                                         @ApiParam("提交报告结束时间") @RequestParam(value = "endTime", required = false) Long endTime,
                                                         @ApiParam("外勤人员关键字") @RequestParam(value = "userName", required = false) String userName,
                                                         @ApiParam("满意度打分") @RequestParam(value = "score", required = false) String score,
                                                         @ApiParam("是否进行满意度回访") @RequestParam(value = "isManaged", required = false) String isManaged,
                                                         @ApiParam(value = "维修站编号") @RequestParam(value = "stationNum", required = false) String stationNum) {
        User user = userRepository.findOne(loginUserId);
        SearchVoId searchVoId;
        UserProfile userProfile = userProfileRepository.findByUserId(user.getId());
        if (user.getUserType().equals(UserType.dealers)) {
            searchVoId = searchPermissionsService.getDealersStationId(userProfile, province, brand);
        } else {
            searchVoId = searchPermissionsService.searchColorReportByProvinceBrand(userProfile, province, brand);
        }
        if (searchVoId.getResultStationId().isEmpty()) {
            return new PageImpl<>(new ArrayList<CustomerRegularReport>(), pageable, 0);
        }

        String[] cities = null;
        String[] categories = null;
        Long[] customerStats = null;
        Long[] customerTypes = null;
        String[] paintBrands = null;
        String[] productPositions = null;
        String[] serviceTypes = null;
        String[] serviceReportStatuses = null;
        String[] reportStatuses = null;
        String[] departments = null;

        if (StringUtils.isNotEmpty(city)){
            cities = city.split(",");
        }
        if (StringUtils.isNotEmpty(category)){
            categories = category.split(",");
        }
        if (StringUtils.isNotEmpty(paintBrand)){
            paintBrands = paintBrand.split(",");
        }
        if (StringUtils.isNotEmpty(productPosition)){
            productPositions = productPosition.split(",");
        }
        if (StringUtils.isNotEmpty(serviceType)){
            serviceTypes = serviceType.split(",");
        }
        if (StringUtils.isNotEmpty(department)){
            departments = department.split(",");
        }
        if (StringUtils.isNotEmpty(serviceReportStatus)){
            serviceReportStatuses = serviceReportStatus.split(",");
        }
        if (StringUtils.isNotEmpty(reportStatus)){
            reportStatuses = reportStatus.split(",");
        }
        if (StringUtils.isNotEmpty(customerStat)){
            customerStats = (Long[]) ConvertUtils.convert(customerStat.split(","),Long.class);
        }
        if (StringUtils.isNotEmpty(customerType)){
            customerTypes = (Long[]) ConvertUtils.convert(customerType.split(","),Long.class);
        }

        QCustomerRegularReport qCustomerRegularReport = QCustomerRegularReport.customerRegularReport;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qCustomerRegularReport.deleted.eq(false));
        builder.and(qCustomerRegularReport.enabled.eq(true));
        builder.and(qCustomerRegularReport.customerReport.isNotNull());
        builder.and(qCustomerRegularReport.customerReport.station.id.in(searchVoId.getResultStationId()));
        if (cities!=null) {
            builder.and(qCustomerRegularReport.customerReport.station.city.in(cities));
        }
        if (customerStats!=null) {
            builder.and(qCustomerRegularReport.customerReport.station.customerStat.in(customerStats));
        }
        if (customerTypes!=null) {
            builder.and(qCustomerRegularReport.customerReport.station.customerType.in(customerTypes));
        }
        if (paintBrands!=null) {
            BooleanBuilder builderOr = new BooleanBuilder();
            for (String str : paintBrands){
                builder.or(qCustomerRegularReport.customerReport.station.paintBrand.like("%" + str + "%"));
            }
            builder.and(builderOr);
        }
        if (productPositions!=null) {
            BooleanBuilder builderOr = new BooleanBuilder();
            for (String str : paintBrands){
                builder.or(qCustomerRegularReport.customerReport.station.productPosition.like("%" + str + "%"));
            }
            builder.and(builderOr);
        }
        if (categories!=null) {
            builder.and(qCustomerRegularReport.customerReport.station.category.in(categories));
        }
        if (StringUtils.isNotEmpty(stationName)) {
            builder.and(qCustomerRegularReport.customerReport.station.name.like("%" + stationName + "%"));
        }
        if (StringUtils.isNotEmpty(stationNum)) {
            builder.and(qCustomerRegularReport.customerReport.station.stationNum.like("%" + stationNum + "%"));
        }

        if (serviceStartTime != null && serviceStartTime != 0){
            builder.and(qCustomerRegularReport.regularReport.startServiceTime.after(new Date(serviceStartTime)));
        }if(serviceEndTime != null && serviceEndTime != 0) {
            builder.and(qCustomerRegularReport.regularReport.endServiceTime.before(new Date(serviceEndTime)));
        }
        if (startTime != null && startTime != 0){
            builder.and(qCustomerRegularReport.creationTime.after(new Date(startTime)));
        }
        if(endTime != null && endTime != 0) {
            builder.and(qCustomerRegularReport.creationTime.before(new Date(endTime)));
        }
        if (serviceTypes != null || serviceReportStatuses != null ) {
            //先从报告答案中找出所有填写过ids中任何一个id的答案
            QStationRegularServiceReport qStationRegularServiceReport = QStationRegularServiceReport.stationRegularServiceReport;
            BooleanBuilder regularBuilder = new BooleanBuilder();
            regularBuilder.and(qStationRegularServiceReport.deleted.eq(false));
            regularBuilder.and(qStationRegularServiceReport.enabled.eq(true));
            BooleanBuilder builderOr = new BooleanBuilder();
            if (serviceTypes != null) {
                for (String id : serviceTypes) {
                    builderOr.or(qStationRegularServiceReport.answerId.like("%" + id + "%"));
                }
                regularBuilder.and(builderOr);
            }
            if (serviceReportStatuses != null){
                for (String id : serviceReportStatuses) {
                    builderOr.or(qStationRegularServiceReport.answerId.like("%" + id + "%"));
                }
                regularBuilder.and(builderOr);
            }
            List<StationRegularServiceReport> list = (List<StationRegularServiceReport>) stationRegularServiceReportRepository.findAll(regularBuilder);
            if (list == null || list.size()==0){
                return new PageImpl<>(new ArrayList<>(), pageable, 0);
            }else {
                List<Long> stationsIDs = new ArrayList<>();
                for (StationRegularServiceReport report : list){
                    stationsIDs.add(report.getStationReport().getStation().getId());
                }
                builder.and(qCustomerRegularReport.regularReport.station.id.in(stationsIDs));
            }
        }
        if (serviceNum != null) {
            builder.and(qCustomerRegularReport.regularReport.station.user.serviceNum.eq(serviceNum));
        }
        if (reportStatuses!=null){
            QStationReport qStationReport = QStationReport.stationReport;
            BooleanBuilder colorBuilder = new BooleanBuilder();
            colorBuilder.and(qStationReport.deleted.eq(false));
            colorBuilder.and(qStationReport.enabled.eq(true));
            BooleanBuilder builderOr = new BooleanBuilder();
            for (String str:reportStatuses){
                builderOr.or(qStationReport.exceptionType.in(str));
            }
            colorBuilder.and(builderOr);
            List<StationReport> list = (List<StationReport>) stationReportRepository.findAll(colorBuilder);
            if (list == null || list.size()==0){
                return new PageImpl<>(new ArrayList<CustomerRegularReport>(), pageable, 0);
            }else {
                List<Long> stationsIDs = new ArrayList<>();
                for (StationReport report : list){
                    stationsIDs.add(report.getStation().getId());
                }
                builder.and(qCustomerRegularReport.regularReport.station.id.in(stationsIDs));
            }
        }
        if (StringUtils.isNotEmpty(userName) || departments != null) {
            QStationColorReport qStationColorReport = QStationColorReport.stationColorReport;
            QUserProfile qUserProfile = QUserProfile.userProfile;
            BooleanBuilder booleanBuilder = new BooleanBuilder();
            booleanBuilder.and(qUserProfile.deleted.eq(false));
            booleanBuilder.and(qUserProfile.enabled.eq(true));
            booleanBuilder.and(qUserProfile.user.userFlag.in(0,2));
            boolean flag = false;
            if (StringUtils.isNotEmpty(userName)) {
                flag = true;
                booleanBuilder.and(qUserProfile.realName.like("%" + userName + "%"));
            }
            if (departments != null){
                flag = true;
                booleanBuilder.and(qUserProfile.user.userType.in(UserType.admin,UserType.dealers,UserType.insiders));
                for (String str:departments){
                    booleanBuilder.or(qUserProfile.departmentList.any().name.eq(str));
                }
            }
            if (flag) {
                List<UserProfile> userProfileList = (List<UserProfile>) userProfileRepository.findAll(booleanBuilder);
                if (userProfileList == null || userProfileList.size() == 0) {
                    return new PageImpl<>(new ArrayList<>(), pageable, 0);
                }
                List<Long> userIds = new ArrayList<>();
                for (UserProfile userp : userProfileList) {
                    userIds.add(userp.getUser().getId());
                }
                BooleanBuilder colorBuilder = new BooleanBuilder();
                colorBuilder.and(qStationColorReport.deleted.eq(false));
                colorBuilder.and(qStationColorReport.enabled.eq(true));
                colorBuilder.and(qStationColorReport.stationReport.station.report_finished.in(1, 2, 3));
                colorBuilder.and(qStationColorReport.stationReport.user.id.in(userIds));
                List<StationColorReport> list = (List<StationColorReport>) stationColorReportRepository.findAll(colorBuilder);
                if (list == null || list.size() == 0) {
                    return new PageImpl<>(new ArrayList<>(), pageable, 0);
                }
                List<Long> ids = new ArrayList<>();
                for (StationColorReport report : list) {
                    if (report.getReportId() != null) {
                        ids.add(Long.parseLong(report.getReportId()));
                    }
                }
                builder.and(qCustomerRegularReport.regularReport.id.in(ids));
            }
        }
        if (paintType != null){
            builder.and(qCustomerRegularReport.regularReport.station.isPaint.eq(paintType?1:0));
        }
        if (payService != null || sentTime!=null || serviceTime!=null){
            QStationColorReport qStationColorReport = QStationColorReport.stationColorReport;
            BooleanBuilder builderStationColor = new BooleanBuilder();
            builderStationColor.and(qStationColorReport.deleted.eq(false));
            builderStationColor.and(qStationColorReport.enabled.eq(true));
            boolean flag = false;
            if (payService!=null){
                flag = true;
                builderStationColor.and(qStationColorReport.answerId.like("%"+payService+"%"));
            }
            if (sentTime!=null){
                flag = true;
                builderStationColor.and(qStationColorReport.answerId.like("%"+sentTime+"%"));
            }
            if (serviceTime!=null){
                flag = true;
                builderStationColor.and(qStationColorReport.answerId.like("%"+serviceTime+"%"));
            }
            if (flag) {
                List<StationColorReport> stationColorReports = (List<StationColorReport>) stationColorReportRepository.findAll(builderStationColor);
                if (stationColorReports == null || stationColorReports.size() == 0) {
                    return new PageImpl<>(new ArrayList<>(), pageable, 0);
                }
                List<Long> Ids = new ArrayList<>();
                for (StationColorReport report : stationColorReports) {
                    Ids.add(report.getStationReport().getStation().getId());
                }
                builder.and(qCustomerRegularReport.regularReport.station.id.in(Ids));
            }
        }

        String[] str = new String[61];
        for (int i = 0; i < str.length; i++) {
            str[i] = i + "";
        }
        String[] notStr = new String[39];
        int index = 61;
        for (int i = 0; i < 39; i++) {
            notStr[i] = index + "";
            index += 1;
        }
        if (StringUtils.isNotEmpty(score)) {
            if (score.equals(Constants.FULL_MARKS)) {
                builder.and(qCustomerRegularReport.score.eq("100"));
            }
            if (score.equals(Constants.MORE_THAN_SIXTY)) {
                builder.and(qCustomerRegularReport.score.in(notStr));
            }
            if (score.equals(Constants.BELOW_THAN_SIXTY)) {
                builder.and(qCustomerRegularReport.score.in(str));
            }
        }
        if (StringUtils.isNotEmpty(isManaged)) {
            if (isManaged.equals(Constants.TRUE)) {
                builder.and(qCustomerRegularReport.managed.eq(true));
            }
            if (isManaged.equals(Constants.FALSE)) {
                builder.and(qCustomerRegularReport.managed.eq(false));
                builder.and(qCustomerRegularReport.score.in(str));
            }
        }
        Sort sort = new Sort(Sort.Direction.DESC, "customerReport.creationTime");
        pageable = new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), sort);
        Page<CustomerRegularReport> customerRegularReportPage = customerRegularReportRepository.findAll(builder, pageable);
        List<CustomerRegularReport> reportList = new ArrayList<>();
        for (CustomerRegularReport report : customerRegularReportPage.getContent()) {
            if (StringUtils.isNotEmpty(score) && score.equals(Constants.BELOW_THAN_SIXTY)) {
                QStationCustomerReport qStationCustomerReport = QStationCustomerReport.stationCustomerReport;
                builder = new BooleanBuilder();
                builder.and(qStationCustomerReport.deleted.eq(false));
                builder.and(qStationCustomerReport.enabled.eq(true));
                builder.and(qStationCustomerReport.stationReport.id.eq(report.getCustomerReport().getId()));
                List<StationCustomerReport> customerReportList = (List<StationCustomerReport>) stationCustomerReportRepository.findAll();
                for (StationCustomerReport r : customerReportList) {
                    QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(r.getAnswerId()));
                    if (answerOption.getName().equals("80%") || answerOption.getName().equals("100%")) {
                        continue;
                    }
                }
            }
            reportList.add(report);
        }
        return new PageImpl<CustomerRegularReport>(reportList, pageable, customerRegularReportPage.getTotalElements());
    }

    @Override
    public Page<CustomerRegularReport> getCustomerReport(Long loginUserId,
                                                         Pageable pageable,
                                                         @ApiParam("省") @RequestParam(value = "provinces", required = false) String province,
                                                         @ApiParam("类型") @RequestParam(value = "category", required = false) String category,
                                                         @ApiParam("品牌") @RequestParam(value = "brandname", required = false) String brand,
                                                         @ApiParam("维修站名称") @RequestParam(value = "stationName", required = false) String stationName,
                                                         @ApiParam("服务开始时间") @RequestParam(value = "serviceStartTime", required = false) Long serviceStartTime,
                                                         @ApiParam("服务结束时间") @RequestParam(value = "serviceEndTime", required = false) Long serviceEndTime,
                                                         @ApiParam("提交报告时间") @RequestParam(value = "startTime", required = false) Long startTime,
                                                         @ApiParam("提交报告结束时间") @RequestParam(value = "endTime", required = false) Long endTime,
                                                         @ApiParam("外勤人员关键字") @RequestParam(value = "userName", required = false) String userName,
                                                         @ApiParam("满意度打分") @RequestParam(value = "score", required = false) String score,
                                                         @ApiParam("60分以下是否已处理") @RequestParam(value = "isManaged", required = false) String isManaged,
                                                         @ApiParam(value = "维修站编号") @RequestParam(value = "stationNum", required = false) String stationNum,
                                                         @ApiParam(value = "部门ID") @RequestParam(value = "departmentId", required = false) Long departmentId,
                                                         @ApiParam(value = "客户状态") @RequestParam(value = "customerStat", required = false) String customerStat,
                                                         @ApiParam(value = "客户类型") @RequestParam(value = "customerType", required = false) String customerType,
                                                         @ApiParam(value = "油漆品牌") @RequestParam(value = "paintBrand", required = false) String paintBrand,
                                                         @ApiParam(value = "品牌定位") @RequestParam(value = "productPosition", required = false) String productPosition) {
        User user = userRepository.findOne(loginUserId);
        SearchVoId searchVoId;
        UserProfile userProfile = userProfileRepository.findByUserId(user.getId());
        if (user.getUserType().equals(UserType.dealers)) {
            searchVoId = searchPermissionsService.getDealersStationId(userProfile, province, brand);
        } else {
            searchVoId = searchPermissionsService.searchColorReportByProvinceBrand(userProfile, province, brand);
        }
        if (searchVoId.getResultStationId().isEmpty()) {
            return new PageImpl<>(new ArrayList<CustomerRegularReport>(), pageable, 0);
        }


        QCustomerRegularReport qCustomerRegularReport = QCustomerRegularReport.customerRegularReport;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qCustomerRegularReport.deleted.eq(false));
        builder.and(qCustomerRegularReport.enabled.eq(true));
        builder.and(qCustomerRegularReport.customerReport.isNotNull());
        builder.and(qCustomerRegularReport.customerReport.station.id.in(searchVoId.getResultStationId()));
        if (StringUtils.isNotEmpty(customerStat)) {
            builder.and(qCustomerRegularReport.customerReport.station.customerStat.eq(Long.valueOf(customerStat)));
        }
        if (StringUtils.isNotEmpty(customerType)) {
            builder.and(qCustomerRegularReport.customerReport.station.customerType.eq(Long.valueOf(customerType)));
        }
        if (StringUtils.isNotEmpty(paintBrand)) {
            builder.and(qCustomerRegularReport.customerReport.station.paintBrand.like("%" + paintBrand + "%"));
        }
        if (StringUtils.isNotEmpty(productPosition)) {
            builder.and(qCustomerRegularReport.customerReport.station.productPosition.like("%" + productPosition + "%"));
        }
        if (StringUtils.isNotEmpty(category)) {
            builder.and(qCustomerRegularReport.customerReport.station.category.eq(category));
        }
        if (StringUtils.isNotEmpty(stationName)) {
            builder.and(qCustomerRegularReport.customerReport.station.name.like("%" + stationName + "%"));
        }
        if (StringUtils.isNotEmpty(stationNum)) {
            builder.and(qCustomerRegularReport.customerReport.station.stationNum.like("%" + stationNum + "%"));
        }

        if (serviceStartTime != null && serviceStartTime != 0 && serviceEndTime != null && serviceEndTime != 0) {
            builder.and(qCustomerRegularReport.regularReport.startServiceTime.after(new Date(serviceStartTime)));
            builder.and(qCustomerRegularReport.regularReport.endServiceTime.before(new Date(serviceEndTime)));
        }
        if (startTime != null && startTime != 0 && endTime != null && endTime != 0) {
            builder.and(qCustomerRegularReport.creationTime.after(new Date(startTime)));
            builder.and(qCustomerRegularReport.creationTime.before(new Date(endTime)));
        }
        if (StringUtils.isNotEmpty(userName) || departmentId != null && departmentId != 0) {
            QUserProfile qUserProfile = QUserProfile.userProfile;
            BooleanBuilder booleanBuilder = new BooleanBuilder();
            booleanBuilder.and(qUserProfile.deleted.eq(false));
            booleanBuilder.and(qUserProfile.enabled.eq(true));
            booleanBuilder.and(qUserProfile.user.userFlag.in(0,2));
            boolean flag = false;
            List<Long> idList = new ArrayList<>();
            if (null != departmentId) {
                flag = true;
                List<Long> parementDeparetment = departmentCoreService.parentDepartment();
                Department department = departmentRepository.findOne(departmentId);
                if (department != null) {
                    if (parementDeparetment.contains(departmentId)) {
                        List<Long> lowerDepartment = departmentCoreService.getLowerDepartment(departmentId);
                        idList.addAll(lowerDepartment);
                    } else {
                        idList.add(departmentId);
                    }
                    booleanBuilder.and(qUserProfile.departmentList.any().id.in(idList));
                }
            }
            if (StringUtils.isNotEmpty(userName)) {
                flag = true;
                booleanBuilder.and(qUserProfile.realName.like("%" + userName + "%"));
            }
            if (flag) {
                List<UserProfile> userProfileList = (List<UserProfile>) userProfileRepository.findAll(booleanBuilder);
                idList = new ArrayList<>();
                for (UserProfile searchUserProfile : userProfileList) {
                    idList.add(searchUserProfile.getUser().getId());
                }
                builder.and(qCustomerRegularReport.regularReport.user.id.in(idList));
            }
        }

        String[] str = new String[61];
        for (int i = 0; i < str.length; i++) {
            str[i] = i + "";
        }
        String[] notStr = new String[39];
        int index = 61;
        for (int i = 0; i < 39; i++) {
            notStr[i] = index + "";
            index += 1;
        }
        if (StringUtils.isNotEmpty(score)) {
            if (score.equals(Constants.FULL_MARKS)) {
                builder.and(qCustomerRegularReport.score.eq("100"));
            }
            if (score.equals(Constants.MORE_THAN_SIXTY)) {
                builder.and(qCustomerRegularReport.score.in(notStr));
            }
            if (score.equals(Constants.BELOW_THAN_SIXTY)) {
                builder.and(qCustomerRegularReport.score.in(str));
            }
        }
        if (StringUtils.isNotEmpty(isManaged)) {
            if (isManaged.equals(Constants.TRUE)) {
                builder.and(qCustomerRegularReport.managed.eq(true));
            }
            if (isManaged.equals(Constants.FALSE)) {
                builder.and(qCustomerRegularReport.managed.eq(false));
                builder.and(qCustomerRegularReport.score.in(str));
            }
        }
        Sort sort = new Sort(Sort.Direction.DESC, "customerReport.creationTime");
        pageable = new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), sort);
        Page<CustomerRegularReport> customerRegularReportPage = customerRegularReportRepository.findAll(builder, pageable);
        List<CustomerRegularReport> reportList = new ArrayList<>();
        for (CustomerRegularReport report : customerRegularReportPage.getContent()) {
            if (StringUtils.isNotEmpty(score) && score.equals(Constants.BELOW_THAN_SIXTY)) {
                QStationCustomerReport qStationCustomerReport = QStationCustomerReport.stationCustomerReport;
                builder = new BooleanBuilder();
                builder.and(qStationCustomerReport.deleted.eq(false));
                builder.and(qStationCustomerReport.enabled.eq(true));
                builder.and(qStationCustomerReport.stationReport.id.eq(report.getCustomerReport().getId()));
                List<StationCustomerReport> customerReportList = (List<StationCustomerReport>) stationCustomerReportRepository.findAll();
                for (StationCustomerReport r : customerReportList) {
                    QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(r.getAnswerId()));
                    if (answerOption.getName().equals("80%") || answerOption.getName().equals("100%")) {
                        continue;
                    }
                }
            }
            reportList.add(report);
        }
        return new PageImpl<CustomerRegularReport>(reportList, pageable, customerRegularReportPage.getTotalElements());
    }
}